package com.tiheima.controller;

import com.tiheima.aop.anno.OperationLogAnno;
import com.tiheima.model.dto.clazz.AddClazzDTO;
import com.tiheima.model.dto.clazz.ClazzPageQuery;
import com.tiheima.model.dto.clazz.UpdateClazzDTO;
import com.tiheima.model.entity.Clazz;
import com.tiheima.model.result.PageResult;
import com.tiheima.model.result.Result;
import com.tiheima.model.vo.clazzListVO;
import com.tiheima.model.vo.clazzVO;
import com.tiheima.service.ClazzService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/clazzs")
public class ClazzController {
    private final ClazzService clazzService;

    public ClazzController(ClazzService clazzService) {
        this.clazzService = clazzService;
    }

    @GetMapping
    public Result<PageResult<clazzListVO>> getClazzList(ClazzPageQuery clazzPageQuery) {
        PageResult<clazzListVO> result = clazzService.getUsersByPage(clazzPageQuery);
        return Result.success(result);
    }

    @OperationLogAnno
    @DeleteMapping("/{id}")
    public Result<?> deleteClazz(@PathVariable("id") Integer id) {
        int row = clazzService.deleteClazz(id);
        return Result.success();
    }

    @OperationLogAnno
    @PostMapping
    public Result<?> addClazz(@RequestBody AddClazzDTO addClazzDTO) {
        clazzService.addClazz(addClazzDTO);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result<clazzVO> getClazzById(@PathVariable("id") Integer id) {
        clazzVO clazzVo = clazzService.getClazzById(id);
        return Result.success(clazzVo);
    }

    @OperationLogAnno
    @PutMapping
    public Result<?> updateClazz(@RequestBody UpdateClazzDTO updateClazzDTO) {
        int row = clazzService.updateClazz(updateClazzDTO);
        return Result.success();
    }

    @GetMapping("/list")
    public Result<List<Clazz>> listClazz() {
        List<Clazz> clazzList = clazzService.listClazz();
        return Result.success(clazzList);
    }
}
